const api = require('../../api/api.js');
const app = getApp();

Page({
  data: {
    goodImg: app.globalData.imgUrl + 'goodImage/',
    place: [],
    type: ''
  },
  onLoad(o) {
    let that = this;
    let id = o.id;
    let _type = o.type;
    wx.getStorage({
      key: 'userId',
      success(res) {
        that.setData({
          userId: res.data
        });
      }
    });
    // let id = '026148191223003191223001191223003001191225001001';
    let siteId = Number.parseInt(id.substring(0, 3));
    let placeId = Number.parseInt(id.substring(3, 6));
    let plantId = id.substring(0, 15);
    let produceId = id.substring(0, 36);
    let goodId = Number.parseInt(id.substring(30, 33));
    this.check(id, _type);
    this.queryGood(goodId);
    this.queryPlaceInfo(placeId);
    this.querySite(siteId);
    that.setData({
      id
    });
  },
  check(id, _type) {
    let that = this;
    if (id.length === 36) {
      if (_type === 'cropProduce') {
        that.setData({
          type: 'crop'
        });
      } else {
        that.setData({
          type: 'livestock'
        });
      }
    } else {
      let harvestId = id.substring(0, 24);
      api.queryHarvest({
        data: {
          harvestId,
          plantId: '',
          type: '',
          account: ''
        },
        success(res) {
          console.log(res.data);
          let harvest = res.data.Harvest[0];
          that.setData({
            type: harvest.type
          });
        }
      });
    }
  },
  queryGood(id) {
    let that = this;
    api.queryGood({
      data: {
        id,
        userId: ''
      },
      success(res) {
        console.log(res.data);
        let good = res.data.good[0];
        good.img = that.data.goodImg + good.img;
        that.setData({
          good
        });
      }
    });
  },
  querySite(id) {
    let that = this;
    api.querySite({
      data: {
        siteId: id
      },
      success(res) {
        console.log(res.data);
        that.setData({
          site: res.data.site
        });
      }
    });
  },
  queryPlaceInfo(id) {
    let that = this;
    api.queryPlaceInfo({
      data: {
        placeId: id,
        userId: ''
      },
      success(res) {
        if (res.data.placeInfos.length === 0) {
          that.queryPlaces(id);
        } else {
          let pi = res.data.placeInfos[0];
          let place = that.data.place;
          place.placeName = pi.name;
          place.produce = pi.produce;
          place.good = pi.good;
          place.remark = pi.remark;
          console.log(place);
          that.setData({
            place,
          });
        }
      }
    });
  },
  queryPlaces(placeId) {
    let that = this;
    api.queryPlaces({
      data: {
        placeId,
        siteId: -1,
        limit: -1,
        pageNumber: 0,
        type: ''
      },
      success(res) {
        console.log(res.data);
        let p = res.data.places[0];
        let place = that.data.place;
        place.placeName = p.placeName;
        place.produce = p.crop;
        place.remark = '暂无简介！';
        console.log(place);
        that.setData({
          place
        });
      }
    });
  },
  goToSiteVideoPage(e) {
    let siteId = e.currentTarget.dataset.siteid;
    wx.navigateTo({
      url: `/pages/video/video?siteid=${siteId}`,
    })
  },
  goToShengChanInfoPage(e) {
    let category = this.data.type;//crop、livestock、fungus
    let id = this.data.id;//溯源ID
    let type = e.currentTarget.dataset.type;//生产种植的过程关键字
    wx.navigateTo({
      url: `/pages/shengchan-info/shengchan-info?type=${type}&category=${category}&id=${id}`,
    })
  }
})